package models

import (
	"errors"
	"gorm.io/gorm"
	"time"
)

type Dept struct {
	DeptId        string    `json:"id" gorm:"column:id; primaryKey;varchar(64);comment:部门id"`
	ParentId      string    `json:"parentId" gorm:"type:varchar(64);comment:上级部门id"`
	DeptName      string    `json:"deptName" gorm:"column:name;type:varchar(100);comment:部门名称"`
	OrderNum      int32     `json:"orderNum" gorm:"type:double;comment:排序"`
	CreateTimeDts time.Time `json:"-" gorm:"type:datetime;column:create_time;comment:创建时间"`
	CreateTime    string    `json:"createTime" gorm:"-"`
	ModifyTimeDts time.Time `json:"-" gorm:"type:datetime;column:update_time;comment:修改时间"`
	ModifyTime    string    `json:"modifyTime" gorm:"-"`
	CreatorId     string    `json:"creatorId" gorm:"type:varchar(64);comment:创建者id"`
	Creator       string    `json:"creator" gorm:"type:varchar(100);comment:创建者"`
	Description   string    `json:"description" gorm:"type:varchar(255);comment:描述"`
	Type          string    `json:"type" gorm:"type:varchar(10);"`
}

func (Dept) Add(db *gorm.DB, data interface{}) (err error) {
	err = db.Model(&Dept{}).Create(data).Error
	return err
}

func (Dept) Mod(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	tx := db.Model(&Dept{}).Scopes(condition...).Updates(data)
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}

func (Dept) FindOne(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	//condition = func(db *gorm.DB) *gorm.DB {
	//	return db.Where("dept_name", "二楼机房")
	//}
	err = db.Model(&Dept{}).
		Scopes(
			condition...,
		).First(data).Error
	return err
}

func (Dept) FindList(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Dept{}).
		Scopes(
			condition...,
		).
		//Joins("JOIN t_user_dept ud ON ud.dept_id = t_dept.dept_id AND ud.user_id = ?", userId).
		Find(data).
		Limit(-1).Offset(-1).Error
	return err
}

func (Dept) FindPage(db *gorm.DB, data interface{}, count *int64, deptIds []interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Dept{}).
		Scopes(condition...).
		Joins("JOIN t_user_dept_r ud ON ud.dept_id = sys_dept.id AND ud.dept_id IN (?)", deptIds).
		Find(data).Limit(-1).Offset(-1).
		Count(count).Error
	return err
}

func (Dept) Del(db *gorm.DB, conds string, params ...interface{}) (err error) {
	tx := db.Model(&Dept{}).Where(conds, params...).Delete(&Dept{})
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}

func (Dept) FindCountByConds(db *gorm.DB, count *int64, conds string, params ...interface{}) error {
	return db.Model(&Dept{}).Where(conds, params...).Find(&Dept{}).Count(count).Error
}

func (Dept) FindListByConds(db *gorm.DB, data interface{}, conds string, params ...interface{}) (err error) {
	return db.Model(&Dept{}).Where(conds, params...).Find(data).Error
}

// FindUserCount 查询角色关联的用户数量
func (Dept) FindUserCount(db *gorm.DB, count *int64, conds string, params ...interface{}) (err error) {
	err = db.Model(&UserDept{}).Where(conds, params...).Find(&UserDept{}).Count(count).Error
	return err
}

func (Dept) TableName() string {
	return "sys_dept"
}

func (e *Dept) Generate() interface{} {
	o := *e
	return &o
}
